package com.qfedu.controller;

import com.github.pagehelper.Page;
import com.qfedu.common.JsonResult;
import com.qfedu.pojo.Job;
import com.qfedu.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/job")
public class JobController {
    @Autowired
    private JobService jobService;

    @RequestMapping("/addJob.do")
    @ResponseBody
    public JsonResult addJob(Job job) {
        jobService.addJod(job);
        return new JsonResult(1,"添加成功");
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public JsonResult delete(Integer id) {
        jobService.deleteJob(id);
        return new JsonResult(1,"删除成功");
    }

    @RequestMapping("/findjob.do")
    @ResponseBody
    public Map<String, Object> selectAll(HttpSession session, Integer page, Integer limit) {
        Job job =(Job) session.getAttribute("login");
        List<Job> list = jobService.selectAll(page,limit);
        long total = ((Page) list).getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count", total);
        map.put("data",list);
        return map;
    }
    @RequestMapping("/update.do")
    @ResponseBody
    private JsonResult updateJob(Job job) {

        System.out.println(job);
        jobService.updateJob(job);
        return new JsonResult(1,"修改成功");
    }
    @RequestMapping("/findById.do")
    @ResponseBody
    public JsonResult findOne(Integer id) {
        JsonResult jsonResult = null;
        try {
            Job job = jobService.findById(id);
            jsonResult = new JsonResult(1,job);
        } catch (Exception e) {
            jsonResult = new JsonResult(0, e.getMessage());
        }

        return jsonResult;
    }
}
